package com.luo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2     //开启swagger2
public class SwaggerConfig {
    @Bean
    // 配置了swagger的docket实例
    public Docket docket(Environment environment){

        //有哪些环境
        Profiles profiles = Profiles.of("dev","prod");

        //获取项目当前的环境
        System.out.println(environment.getActiveProfiles()[0]);
        //判断自己是否处于自己设定的环境当中
        boolean b = environment.acceptsProfiles(profiles);
        System.out.println(b);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .host("localhost:8080")
                .groupName("Luo Qianyi") //设置api的分组,分组不能重名，默认按ASCI码从小到大排序展示
                 .enable(b) //设置false则设置了swagger不能被浏览器访问
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.luo.controller")) //RequestHandlerSelectors配置扫描接口的方式
                //.paths(PathSelectors.ant("/luo/**"))//过滤留下什么样的路径接口
                .build();
    }
    //设置多个分组
    //@Bean
    //    //public Docket docket1(){
    //    //    return new Docket(DocumentationType.SWAGGER_2).groupName("a");
    //    //}

    //配置Swagger信息
    private ApiInfo apiInfo(){
        return new ApiInfo(
                "落迁忆的SwaggerApi文档",
                "落叶归根迁故里，忆苦思甜随风去",
                "999.999",
                "#",
                new Contact("落迁忆","https://www.luoqianyi.love/about","3228077562@qq.com"),//作者信息
                "Apache 2.0",
                "#",
                new ArrayList());
    }
}
